package com.liangge.essaysite.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.ehcache.Cache;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.liangge.essaysite.bean.Article;
import com.liangge.essaysite.bean.Category;
import com.liangge.essaysite.service.ArticleService;
import com.liangge.essaysite.service.CategoryService;
import com.liangge.essaysite.util.AgentKeys;
import com.liangge.essaysite.util.Pagination;

@Controller
@RequestMapping("/index")
public class IndexController {
	private static Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	private ArticleService articleService;
	@Autowired @Qualifier("articleService")
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	
	private CategoryService categoryService;
	@Autowired @Qualifier("categoryService")
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	private Cache ehCache;
	@Autowired @Qualifier("ehCacheDefault")
	public void setEhCache(Cache ehCache) {
		this.ehCache = ehCache;
	}

	/**
	 * 首页最新列表
	 * @author liangge
	 */
	@RequestMapping(value="/new/page/{pageNo}",method=RequestMethod.GET)
	public String indexNew(@PathVariable String pageNo,Model model){
		//页码控制过滤
		int pNo = 1;
		try {
			pNo = Integer.parseInt(pageNo);
		} catch (Exception e) {
		}
		Pagination<Article> pagination = articleService.selectNewArticleList(pNo);
		model.addAttribute("pagination", pagination);
		model.addAttribute("current", "new");
		return "index/newlist.ftl";
	}
	
	/**
	 * 首页点击列表
	 * @author liangge
	 */
	@RequestMapping(value="/read/{limitDays}/page/{pageNo}",method=RequestMethod.GET)
	public String indexRead(@PathVariable String limitDays,@PathVariable String pageNo,Model model){
		//页码控制过滤
		int pNo = 1;
		try {
			pNo = Integer.parseInt(pageNo);
		} catch (Exception e) {
		}
		//limitDays控制过滤
		int lDays = 3;
		try{
			lDays = Integer.parseInt(limitDays);
		}catch(Exception e){
		}
		Pagination<Article> pagination = articleService.selectReadArticleList(pNo, lDays);
		model.addAttribute("pagination", pagination);
		model.addAttribute("current", "read");
		model.addAttribute("postdays",limitDays);
		return "index/readlist.ftl";
	}
	
	/**
	 * 首页精华列表
	 * @author liangge
	 */
	@RequestMapping(value="/good/{limitDays}/page/{pageNo}",method=RequestMethod.GET)
	public String indexGood(@PathVariable String limitDays,@PathVariable String pageNo,Model model){
		//页码控制过滤
		int pNo = 1;
		try {
			pNo = Integer.parseInt(pageNo);
		} catch (Exception e) {
		}
		//limitDays控制过滤
		int lDays = 3;
		try{
			lDays = Integer.parseInt(limitDays);
		}catch(Exception e){
		}
		Pagination<Article> pagination = articleService.selectGoodArticleList(pNo, lDays);
		model.addAttribute("pagination", pagination);
		model.addAttribute("current", "good");
		model.addAttribute("postdays",limitDays);
		return "index/goodlist.ftl";
	}
	
	/**
	 * 首页回复列表
	 * @author liangge
	 */
	@RequestMapping(value="/comment/{limitDays}/page/{pageNo}",method=RequestMethod.GET)
	public String indexComment(@PathVariable String limitDays,@PathVariable String pageNo,Model model){
		//页码控制过滤
		int pNo = 1;
		try {
			pNo = Integer.parseInt(pageNo);
		} catch (Exception e) {
		}
		//limitDays控制过滤
		int lDays = 3;
		try{
			lDays = Integer.parseInt(limitDays);
		}catch(Exception e){
		}
		Pagination<Article> pagination = articleService.selectCommentArticleList(pNo, lDays);
		model.addAttribute("pagination", pagination);
		model.addAttribute("current", "comment");
		model.addAttribute("postdays",limitDays);
		return "index/commentlist.ftl";
	}
	
	/**
	 * 首页分类文章列表
	 * @author liangge
	 */
	@RequestMapping(value="/category/{categoryId}/page/{pageNo}",method=RequestMethod.GET)
	public String indexCategory(@PathVariable String categoryId,@PathVariable String pageNo,Model model){
		//分类主键过滤
		int cId = 1;
		try{
			cId = Integer.parseInt(categoryId);
		}catch(Exception e){
		}
		//页码控制过滤
		int pNo = 1;
		try {
			pNo = Integer.parseInt(pageNo);
		} catch (Exception e) {
		}
		Pagination<Article> pagination = articleService.selectCategoryArticleList(pNo, cId);
		List<Category> categoryList = categoryService.selectCategoryList();
		model.addAttribute("categorys", categoryList);
		model.addAttribute("categoryId", cId);
		model.addAttribute("pagination", pagination);
		model.addAttribute("current", "category");
		return "index/categorylist.ftl";
	}
	
	/**
	 * 首页发表文章
	 * @author liangge
	 */
	@RequestMapping(value="/post",method=RequestMethod.GET)
	public String indexPost(Model model){
		List<Category> categoryList = categoryService.selectCategoryList();
		model.addAttribute("categorys", categoryList);
		model.addAttribute("current", "post");
		return "index/post.ftl";
	}
	/**
	 * 首页发表文章
	 * @author liangge
	 */
	@RequestMapping(value="/post",method=RequestMethod.POST)
	@ResponseBody
	public String indexPostPost(HttpServletRequest request){
		JSONObject json = new JSONObject();
		try {
			Article article = new Article();
			article.setArticleSource(AgentKeys.ARTICLE_SOURCE_POSTED);
			//设置分类
			int categoryId = Integer.parseInt(request.getParameter("category"));
			article.setCategory(new Category(categoryId));
			//设置标题
			String title = request.getParameter("title");
			article.setArticleName(title);
			//设置内容
			String postText = request.getParameter("post_text");
			article.setContent(postText);
			//设置邮箱
			String email = request.getParameter("email");
			article.setEmail(email);
			//设置状态下线
			article.setStatus(AgentKeys.ARTICLE_STATUS_OFFLINE);
			
			int insert = articleService.insertArticle(article);
			if(insert == 1){
				json.put("flag", "1");
			}else{
				json.put("flag", "0");
			}
		} catch (Exception e) {
			json.put("flag", "0");
			e.printStackTrace();
		}
		return json.toJSONString();
	}
}
